import { Component, instantiate, Node, Vec2, Vec3, _decorator } from 'cc';
import { HeatMapPoint2D } from './HeatMapPoint2D';
const { ccclass, property } = _decorator;

interface IHeatmap2PointData {
    x: number;
    y: number;
    intensity: number;
}

@ccclass('Render2DHeatPoint')
export class Render2DHeatPoint extends Component {
    @property
    public mapWidth = 200;

    @property
    public mapHeight = 200;

    @property(Node)
    public holeNode: Node = null;

    @property(Node)
    public bgNode: Node = null;

    public start() {
        const heatPointRes = this.getHeatPointArr();
        heatPointRes.forEach((i) => {
            if (i.intensity > 1) {
                this.drawCircle(new Vec3(i.x, i.y, 1), i.intensity);
            }
        });
    }

    public update(deltaTime: number) {}

    public getHeatPointArr() {
        const heatMapData = [
            [1, 2, 3, 4, 5, 16, 7, 8, 9, 20],
            [2, 0, 3, 5, 16, 1, 40, 9, 10, 8],
            [0, 0, 3, 4, 6, 7, 30, 9, 10, 8],
            [4, 2, 3, 5, 5, 1, 30, 4, 0, 18],
            [0, 2, 3, 5, 16, 7, 30, 1, 0, 8],
            [6, 2, 3, 5, 6, 17, 1, 39, 10, 0],
        ];

        const gridSizeX = this.mapWidth / heatMapData[0].length;
        const gridSizeZ = this.mapHeight / heatMapData.length;
        const mapCenterPos = new Vec2(this.mapWidth / 2, this.mapHeight / 2);

        let res: IHeatmap2PointData[] = [];

        for (let i = 0; i < heatMapData.length; i++) {
            for (let j = 0; j < heatMapData[i].length; j++) {
                let x = gridSizeX * j - mapCenterPos.x + gridSizeX / 2;
                let y = gridSizeZ * i - mapCenterPos.y + gridSizeZ / 2;
                res.push({ x: x, y: -y, intensity: heatMapData[i][j] });
            }
        }
        return res;
    }

    public drawCircle(pos: Vec3, intensity: number) {
        let heatPointNode = instantiate(this.holeNode);
        heatPointNode.active = true;
        this.bgNode.addChild(heatPointNode);

        heatPointNode.getComponent(HeatMapPoint2D).setIntensity(intensity / 40);
        heatPointNode.setPosition(pos.x, pos.y, 0);
    }
}
